﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Linq;
using EasierRdp.Base;
using EasierRdp.Util;

namespace EasierRdp.Models
{
    class RdpSetting : RdpModel
    {
        public const string
            AttributeFullScreen = "Full",
            AttributeAllMonitors = "All",
            AttributeWidth = "Width",
            AttributeHeight = "Height",
            ElementName = "Setting";

        public bool FullScreen { get; set; }

        public bool UseAllMonitors { get; set; }

        public int? Height { get; set; }

        public int? Width { get; set; }

        public override IEnumerable<INode> Children
        {
            get { return Enumerable.Empty<INode>(); }
        }

        public RdpSetting()
        {
            
        }

        public RdpSetting(XElement element)
        {
            if (element != null)
            {
                Name = element.GetString(AttributeName);
                Id = element.GetInt(AttributeId);
                Width = element.GetNullableInt(AttributeWidth);
                Height = element.GetNullableInt(AttributeHeight);
                FullScreen = element.GetBoolean(AttributeFullScreen);
                UseAllMonitors = element.GetBoolean(AttributeAllMonitors);
                Expanded = element.GetBoolean(AttributeExpanded);
                return;
            }
            throw new ArgumentNullException("element");
        }

        public override XElement ToXml()
        {
            return new XElement(ElementName
                , new XAttribute(AttributeName, Name)
                , new XAttribute(AttributeId, Id)
                , new XAttribute(AttributeExpanded, Expanded)
                , Width.HasValue ? new XAttribute(AttributeWidth, Width) : null
                , Height.HasValue ? new XAttribute(AttributeHeight, Height) : null
                , new XAttribute(AttributeFullScreen, FullScreen)
                , new XAttribute(AttributeAllMonitors, UseAllMonitors)
                );
        }

        public RdpSetting Clone()
        {
            var s = (RdpSetting)this.MemberwiseClone();
            s.Id = 0;
            return s;
        }
    }

    static class RdpSettingExtension
    {
        public static IEnumerable<XElement> ToXml(this IEnumerable<RdpSetting> src)
        {
            return src == null ? null : src.Select(s => s.ToXml());
        }

        public static void FromXml(this IList<RdpSetting> src, XElement parentElement, string elementName)
        {
            foreach (var item in parentElement.Elements(elementName).Select(e => new RdpSetting(e)))
            {
                src.Add(item);
            }
        }
    }
}
